package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Comunidad;
import com.lostreseditores.sabersai.model.dto.ComunidadDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class ComunidadView {
    private InputText txtComudescripv;
    private InputText txtComuidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<ComunidadDTO> data;
    private ComunidadDTO selectedComunidad;

    public ComunidadView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            ComunidadDTO comunidadDTO = (ComunidadDTO) e.getObject();

            if (txtComudescripv == null) {
                txtComudescripv = new InputText();
            }

            txtComudescripv.setValue(comunidadDTO.getComudescripv());

            if (txtComuidn == null) {
                txtComuidn = new InputText();
            }

            txtComuidn.setValue(comunidadDTO.getComuidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtComudescripv != null) {
            txtComudescripv.setValue(null);
            txtComudescripv.setDisabled(true);
        }

        if (txtComuidn != null) {
            txtComuidn.setValue(null);
            txtComuidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Comunidad entity = null;

        try {
            Integer comuidn = new Integer(txtComuidn.getValue().toString());
            entity = BusinessDelegatorView.getComunidad(comuidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtComudescripv.setDisabled(false);
            txtComuidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtComudescripv.setValue(entity.getComudescripv());
            txtComudescripv.setDisabled(false);
            txtComuidn.setValue(entity.getComuidn());
            txtComuidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveComunidad(FacesUtils.checkString(
                    txtComudescripv), FacesUtils.checkInteger(txtComuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteComunidad(FacesUtils.checkInteger(
                    txtComuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateComunidad(FacesUtils.checkString(
                    txtComudescripv), FacesUtils.checkInteger(txtComuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtComuidn == null) {
                txtComuidn = new InputText();
            }

            txtComuidn.setValue(selectedComunidad.getComuidn());

            BusinessDelegatorView.deleteComunidad(FacesUtils.checkInteger(
                    txtComuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedComunidad);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String comudescripv, Integer comuidn)
        throws Exception {
        try {
            BusinessDelegatorView.updateComunidad(comudescripv, comuidn);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("ComunidadView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtComudescripv() {
        return txtComudescripv;
    }

    public void setTxtComudescripv(InputText txtComudescripv) {
        this.txtComudescripv = txtComudescripv;
    }

    public InputText getTxtComuidn() {
        return txtComuidn;
    }

    public void setTxtComuidn(InputText txtComuidn) {
        this.txtComuidn = txtComuidn;
    }

    public List<ComunidadDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataComunidad();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<ComunidadDTO> comunidadDTO) {
        this.data = comunidadDTO;
    }

    public ComunidadDTO getSelectedComunidad() {
        return selectedComunidad;
    }

    public void setSelectedComunidad(ComunidadDTO comunidad) {
        this.selectedComunidad = comunidad;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
